package aspect.example;
import aspect.physics.Collider;
import aspect.entity.behavior.Behavior;
import static aspect.core.AspectLauncher.*;
import static aspect.core.AspectRenderer.*;
import static aspect.core.AspectRenderer.RenderMode.ORTHOGRAPHIC;
import static aspect.core.AspectRenderer.RenderMode.PERSPECTIVE;
import aspect.entity.Entity;
import aspect.entity.Player;
import aspect.event.KeyEvent;
import aspect.event.MouseEvent;
import aspect.physics.PhysicsConstants;
import aspect.physics.RigidBody;
import aspect.physics.Time;
import aspect.physics.dynamics.Force;
import aspect.physics.dynamics.PointGravity;
import aspect.physics.dynamics.UniformGravity;
import aspect.render.Canvas;
import aspect.render.FrameBuffer;
import aspect.render.Light;
import aspect.render.Material;
import aspect.render.Mesh;
import aspect.render.ViewModel;
import aspect.render.shader.Shader;
import aspect.render.shader.ShaderProgram;
import static aspect.resources.Resources.*;
import aspect.util.Angles;
import aspect.util.Color;
import aspect.util.Matrix3x3;
import aspect.util.Transform;
import aspect.util.Trig;
import static org.lwjgl.input.Keyboard.*;
import aspect.util.Vector3;
import aspect.world.ListWorld;
import aspect.world.World;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.File;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
public class AspectTest extends Behavior {
//private Entity player;
private Entity spawner;
private UniformGravity gravity;
private Random random = new Random();
private int numBlocks = 0;
private Canvas counter;
public static void main(String[] args) {
run(800, 600, false, 60, new AspectTest());
printFPS(1000);
}
@Override
public void onAdd() {
World.main.clear();
Light.clear();
//floor = new Entity(box(new Color(0.0f, 0.8f, 0.2f), 20f, 20f, 20f));
Vector3[] fv = new Vector3[8];
fv[0] = new Vector3(-10.0f, 0.0f, -10.0f);
fv[1] = new Vector3(-10.0f, 0.0f, 10.0f);
fv[2] = new Vector3(10.0f, 0.0f, 10.0f);
fv[3] = new Vector3(10.0f, 0.0f, -10.0f);
fv[4] = new Vector3(-5.0f, 5.0f, -5.0f);
fv[5] = new Vector3(-5.0f, 5.0f, 5.0f);
fv[6] = new Vector3(5.0f, 5.0f, 5.0f);
fv[7] = new Vector3(5.0f, 5.0f, -5.0f);
Vector3[] fn = new Vector3[5];
fn[0] = Vector3.yAxis();
fn[1] = new Vector3(1.0f, 1.0f, 0.0f).normalize();
fn[2] = new Vector3(0.0f, 1.0f, 1.0f).normalize();
fn[3] = new Vector3(0.0f, 1.0f, -1.0f).normalize();
fn[4] = new Vector3(-1.0f, 1.0f, 0.0f).normalize();
Vector3[] fe = new Vector3[6];
fe[0] = Vector3.xAxis();
fe[1] = Vector3.zAxis();
fe[2] = new Vector3(1.0f, 1.0f, 1.0f).normalize();
fe[3] = new Vector3(1.0f, 1.0f, -1.0f).normalize();
fe[4] = new Vector3(-1.0f, 1.0f, -1.0f).normalize();
fe[5] = new Vector3(-1.0f, 1.0f, 1.0f).normalize();
ViewModel floorModel = loadObjModel(new File("models/platform.obj"), Vector3.one());
Entity floor = new Entity(floorModel);
floor.transform.position = new Vector3(0, -7, 0);
//floor.transform.set(new Vector3(0, -12, 0), Vector3.yAxis().negate(), Vector3.zAxis(), Vector3.one());
floor.addBehavior(new Collider(fv, fn, fe));
World.main.add(floor);
Light light = new Light(Color.WHITE);
light.transform.position = new Vector3(-5, 5, 2);
light.setAttenuation(1, 0, 0);
//player = new Player(new Vector3(0, 0, 4));
//World.main.add(player);
camera = new Transform(new Vector3(-10, 10, 10), new Vector3(1, -1, -1), Vector3.yAxis(), Vector3.one());
gravity = new UniformGravity();
World.main.addForce(gravity);
counter = new Canvas(128, 32);
updateCounter();
spawner = new Entity();
spawner.transform.position.y = 15.0f;
spawner.addBehavior(new Behavior() {
private float timeUntilSpawn = 2.0f;
@Override
public void update() {
timeUntilSpawn -= Time.deltaTime();
if (timeUntilSpawn <= 0) {
float width = random.nextFloat() * 2.0f + 1.0f;
float height = random.nextFloat() * 2.0f + 1.0f;
float depth = random.nextFloat() * 2.0f + 1.0f;
final Entity entity = RigidBody.box(new Material(Color.random()), width, height, depth, 1.0f);
entity.addBehavior(new Behavior() {
@Override
public void update() {
if (entity.transform.position.y < -20.0f || isKeyDown(KEY_LCONTROL)) {
entity.destroy();
numBlocks--;
updateCounter();
}
}
});
entity.transform.position = spawner.transform.position;
entity.transform.forward = new Vector3(random.nextFloat(), random.nextFloat(), random.nextFloat());
entity.transform.setUp(new Vector3(random.nextFloat(), random.nextFloat(), random.nextFloat()));
//entity.transform.up.x = 0.1f;
World.main.add(entity);
numBlocks++;
updateCounter();
timeUntilSpawn = 2.0f;
}
}
});
World.main.add(spawner);
addKeyListener(this);
}
private void updateCounter() {
Graphics2D g = counter.getGraphics();
counter.clearAlpha();
g.setColor(java.awt.Color.RED);
g.setFont(new Font("Lucidia Console", Font.PLAIN, 24));
g.drawString("Blocks: " + numBlocks, 5, 30);
counter.render();
}
@Override
public void render() {
ViewModel model = counter.getModel();
model.transform.position = new Vector3(96, getCanvasHeight() - 32);
setRenderMode(ORTHOGRAPHIC);
model.render();
setRenderMode(PERSPECTIVE);
}
@Override
public void keyEvent(KeyEvent evt) {
if (evt.key == KEY_SPACE && evt.type) {
stop();
}
}
private void stop() {
if (gravity.acc.equals(Vector3.zero())) {
gravity.acc = new Vector3(0, -9.8f, 0);
} else {
gravity.acc = Vector3.zero();
}
}
}